/*
 * FileUtil.java
 *
 * Created on February 4, 2009, 9:44 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.vacuela.phonecredits.util;

import com.vacuela.phonecredits.PhoneProfile;
import java.io.FileInputStream;
import java.util.Set;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import static  com.vacuela.phonecredits.constant.PhoneConstant.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author nestor.carrasco
 */
public class FileUtil {
    
    /** Creates a new instance of FileUtil */
    public FileUtil() {
    }
    
    public static List<PhoneProfile> readPhonesFromFile(String filePath){
        List<PhoneProfile> phoneList = new ArrayList<PhoneProfile>();
        POIFSFileSystem fs      =null;
        try{
            fs      =
                    new POIFSFileSystem(
                    new FileInputStream(filePath));
            HSSFWorkbook wb = new HSSFWorkbook(fs);
            HSSFSheet sheet = wb.getSheetAt(0);
            int rowIndex = 1;
            HSSFRow row = sheet.getRow(rowIndex);
            if ( row == null){
                return phoneList;
            }
            
            String phoneNumber = row.getCell(NUMBER_EXCEL_FIELD).getStringCellValue();
            String nip = row.getCell(NIP_EXCEL_FIELD).getStringCellValue();
			String sim = row.getCell(SIM_EXCEL_FIELD).getStringCellValue();
            while(phoneNumber != null && !phoneNumber.trim().equals("")&& nip != null &&  !nip.trim().equals("")){
                PhoneProfile phoneEO = new  PhoneProfile(phoneNumber.trim(),nip.trim(),sim.trim());
                phoneList.add(phoneEO);         
                //here the persintence           
                
                ++rowIndex;
                row = sheet.getRow(rowIndex);
                if ( row == null){
                    break;
                }
                phoneNumber = row.getCell(NUMBER_EXCEL_FIELD).getStringCellValue();
                nip =row.getCell(NIP_EXCEL_FIELD).getStringCellValue();
                sim = row.getCell(SIM_EXCEL_FIELD).getStringCellValue();
            }
        }catch(Exception e){
            System.out.println("Exception during Import ==> " + e);
        }
        return phoneList;
    }
    public static void main(String []arg){
        //Set<PhoneProfile> phoneSet = readPhonesFromFile("C:\\Documents and Settings\\nestor.carrasco.SOFTTEKGE\\Desktop\\crons\\phoneCreditsReport.xls");
        //System.out.print("Vlue" +phoneSet );
    }
}
